<?php
declare (strict_types=1);

namespace app\common\model;

class Relation extends Base
{
    protected $name = 'user_relation';

    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'uid')
            ->field('id,code,nickname,mobile,photo,parent_id,parent_code,is_active,level_id,my_total,team_total');
    }

    protected function fresh(): object
    {
        return $this->hasOne(User::class, 'id', 'new_uid')
            ->field('id,code,nickname,mobile,photo,parent_id,parent_code,is_active,level_id,my_total,team_total');
    }

    /**
     * 记录推荐关系
     */
    public static function generate($new_uid, $new_code, $parent_id, $parent_code): bool
    {
        try {
            parent::create([
                'uid' => $parent_id,
                'code' => $parent_code,
                'algebra' => 1,
                'new_uid' => $new_uid,
                'new_code' => $new_code,
            ]);
            $time = time();
            parent::where('new_uid', $parent_id)
                ->field('uid,code,algebra + 1 as algebra,' . $new_uid . ' as new_uid,' . $new_code . ' as new_code,' . $time . ' as create_time,' . $time . ' as update_time')
                ->selectInsert([
                    'uid',
                    'code',
                    'algebra',
                    'new_uid',
                    'new_code',
                    'create_time',
                    'update_time'
                ], (new static())->getName());

        } catch (\Exception $e) {

            return false;
        }

        return true;
    }

    /**
     * 更新推荐关系
     */
    public static function renew($uid, $parent_code, $admin): array
    {
        try {
            if ($uid == 1) {
                throw new \Exception('禁止修改');
            }
            $user = User::where(['id' => $uid])->find();
            if ($user->parent_code != $parent_code) {
                $parent = User::where(['code' => $parent_code, 'is_active' => 2, 'is_del' => 0])->find();
                if (!$parent) {
                    throw new \Exception('新推荐人不存在或未激活');
                }
                $count = parent::where(['uid' => $user->id, 'new_uid' => $parent->id])->count();
                if ($count) {
                    throw new \Exception('新推荐人不能为伞下成员');
                }
                $msg = '会员【' . $user->code . '】的推荐人由【' . $user->parent_code . '】改为【' . $parent->code . '】';
                //更新当前会员的信息
                $user->parent_id = $parent->id;
                $user->parent_code = $parent->code;
                $user->save();
                //删除当前会员的原推荐关系
                parent::where(['new_uid' => $user->id])->delete();
                //建立当前会员的新推荐关系
                (new Relation())->record($user->id, $user->code, $user->parent_id, $user->parent_code);
                //查询伞下会员信息
                $child = parent::where(['uid' => $user->id])->order('new_uid asc')->select();
                foreach ($child as $val) {
                    //删除伞下会员的原推荐关系
                    parent::where(['new_uid' => $val->fresh->id])->delete();
                    //建立伞下会员的新推荐关系
                    (new Relation())->record($val->fresh->id, $val->fresh->code, $val->fresh->parent_id, $val->fresh->parent_code);
                }
                Logs::adds($msg, $admin->id, $admin->admin_name);
            }

        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '操作成功'];
    }

    /**
     * 更新团队信息
     */
    public static function renewTeamTotal($amount, $uid, $key = 'team_total'): bool
    {
        try{
            $all_id = parent::where('new_uid', $uid)->column('uid');
            if (!empty($all_id)) {
                User::whereIn('id', $all_id)->inc($key, floatval($amount))->update();
            }

        } catch (\Exception $e) {

            return false;
        }

        return true;
    }
}